import { request } from '@/utils'

/**
 * @description 登录接口
 * @param accountName 登录名
 * @param accountPass 登录密码
 * @returns Promise
 */

export const login = (accountName: string, accountPass: string) => {
  return request({
    url: '/login',
    method: 'GET',
    params: {
      accountName,
      accountPass
    }
  })
}

/**
 *
 * @param phone 手机号
 * @param smsCode 短信验证码
 * @returns Promise
 */
export const smsLogin = (phone: string, smsCode: string) => {
  return request({
    url: '/smsLogin',
    method: 'GET',
    params: {
      phone,
      smsCode
    }
  })
}

/**
 * @description 通过短信修改密码
 * @param phone 手机号
 * @param smsCode 验证码
 * @param password 新密码
 * @returns
 */
export const forgetPassWord = (
  phone: string,
  smsCode: string,
  password: string
) => {
  return request({
    url: '/forgetPassWord',
    method: 'GET',
    params: {
      phone,
      smsCode,
      password
    }
  })
}

/**
 * @description 登录成功后修改密码
 * @param password 新密码
 * @returns
 */
export const updatePassWord = (password: string) => {
  return request({
    url: '/updatePassWord',
    method: 'GET',
    params: {
      password
    }
  })
}

/**
 * @description 获取首页菜单
 * @returns Promise
 */

export const getMenuList = () => {
  return request({
    url: '/getMenuInfoByUser',
    method: 'GET'
  })
}

/**
 * @description 管理端查询评价信息
 * @returns Promise
 */

export const getBatchInfo = () => {
  return request({
    url: '/getBatchInfo',
    method: 'GET'
  })
}

/**
 * @description 查询评价参与机构信息
 * @param code 	评价批次编号
 * @returns Promise
 */

export const getBatchUnitInfo = (code: string) => {
  return request({
    url: '/getBatchUnitInfo',
    method: 'GET',
    params: {
      code
    }
  })
}

// 新建评价批次信息
export const addBatchInfo = (params: {}) => {
  return request({
    url: '/addBatchInfo',
    method: 'POST',
    params
  })
}

// 修改评价批次信息
export const updateBatchInfo = (params: {}) => {
  return request({
    url: '/updateBatchInfo',
    method: 'POST',
    params
  })
}

/**
 * @description 获取评价指标
 * @returns Promise
 */

export const getCriteriaInfo = () => {
  return request({
    url: '/getCriteriaInfo',
    method: 'GET'
  })
}

/**
 * @description 获取学段
 * @returns Promise
 */

export const getGradeInfo = () => {
  return request({
    url: '/getGradeInfo',
    method: 'GET'
  })
}

/**
 * @description 获取机构
 * @param grade 学段
 * @returns Promise
 */

export const getUnitInfo = (grade?: string) => {
  return request({
    url: '/getUnitInfo',
    method: 'GET',
    params: {
      grade
    }
  })
}

/**
 * @description 查询指标详细信息
 * @param criteriaCode 评价指标编号
 * @returns Promise
 */

export const getCriteriaDetailInfo = (criteriaCode: string) => {
  return request({
    url: '/getCriteriaDetailInfo',
    method: 'GET',
    params: {
      criteriaCode
    }
  })
}

/**
 * @description 删除评价信息
 * @param code 评价批次编号
 * @returns Promise
 */

export const deleteBatchInfo = (code: string) => {
  return request({
    url: '/deleteBatchInfo',
    method: 'GET',
    params: {
      code
    }
  })
}

/**
 * @description 查询评价预警信息
 * @param code 评价批次编号
 * @returns Promise
 */
export const getBatchWarningInfo = (code: string) => {
  return request({
    url: '/getBatchWarningInfo',
    method: 'GET',
    params: {
      code
    }
  })
}

// 新增评价预警信息
export const addBatchWarningInfo = (params: {}) => {
  return request({
    url: '/addBatchWarningInfo',
    method: 'POST',
    params
  })
}

/**
 * @description 删除评价预警信息
 * @param id 评价预警信息id
 * @param code 评价批次编号
 * @returns Promise
 */
export const deleteBatchWarningInfo = (id: string, code: string) => {
  return request({
    url: '/deleteBatchWarningInfo',
    method: 'GET',
    params: {
      id,
      code
    }
  })
}

/**
 * @description 查询评价批次题目信息
 * @param batchCode 评价批次编号
 * @param type 评分方式[01:量表导入;02:手动输入;03:专家评分]
 * @returns Promise
 */
export const getBatchQuestionInfo = (batchCode: string, type: string) => {
  return request({
    url: '/getBatchQuestionInfo',
    method: 'GET',
    params: {
      batchCode,
      type
    }
  })
}

/**
 * @description 保存题目用户上传信息
 * @param batchCode 评价批次编号
 * @param code 题目编号
 * @param type 上传的资料类型
 * @param url 资料上传成功后路径
 * @param userCode 用户编号
 * @returns Promise
 */
export const saveUserAnswer = (
  batchCode: string,
  code: string,
  type: string,
  url: string,
  userCode: string,
  fileSize: number
) => {
  return request({
    url: '/saveUserAnswer',
    method: 'GET',
    params: {
      batchCode,
      code,
      type,
      url,
      userCode,
      fileSize
    }
  })
}

/**
 * @description 查询用户题目上传信息
 * @param batchCode 评价批次编号
 * @param userCode  用户编号
 * @returns Promise
 */
export const getUserAnswer = (batchCode: string, userCode: string) => {
  return request({
    url: '/getUserAnswer',
    method: 'GET',
    params: {
      batchCode,
      userCode
    }
  })
}

/**
 * @description 获取七牛token，用户资料上传
 * @returns Promise
 */
export const getQiniuToken = () => {
  return request({
    url: '/getQiniuToken',
    method: 'GET'
  })
}

/**
 * @description 删除用户上传的资料信息
 * @param batchCode 评价批次编号
 * @param code 题目编号
 * @param type 上传的资料类型
 * @param userCode 用户编号
 * @returns Promise
 */
export const deleteUserAnswer = (
  batchCode: string,
  code: string,
  type: string,
  userCode: string,
  fileName: string
) => {
  return request({
    url: '/deleteUserAnswer',
    method: 'GET',
    params: {
      batchCode,
      code,
      type,
      userCode,
      fileName
    }
  })
}

/**
 * @description 查询打分板指标信息
 * @param batchCode 评价批次编号
 * @param type 评分方式[01:量表导入;02:手动输入;03:专家评分]
 * @returns Promise
 */
export const getPointInfo = (batchCode: string, type?: string) => {
  return request({
    url: '/getPointInfo',
    method: 'GET',
    params: {
      batchCode,
      type
    }
  })
}

/**
 * @description 查询打分板指标打分点信息
 * @param batchCode 评价批次编号
 * @param parentCode 父节点编号
 * @param type 打分方式
 * @returns Promise
 */
export const getPointInfoByParentCode = (
  batchCode: string,
  parentCode: string,
  type: string
) => {
  return request({
    url: '/getPointInfoByParentCode',
    method: 'GET',
    params: {
      batchCode,
      parentCode,
      type
    }
  })
}

/**
 * @description 查询打分板学校信息
 * @param batchCode 评价批次编号
 * @param questionCode 指标code
 * @param grade 学段
 * @param unitName 学校名称
 */

interface ParamsType {
  batchCode: string
  questionCode: string
  grade?: string
  unitName?: string
}

export const getSchoolInfo = (params: ParamsType) => {
  const { batchCode, questionCode, grade, unitName } = params
  return request({
    url: '/getSchoolInfo',
    method: 'GET',
    params: {
      batchCode,
      questionCode,
      grade,
      unitName
    }
  })
}

/**
 * @description 查询评分规则
 * @returns Promise
 */
export const getScoringRules = (ruleCode: string = '50') => {
  return request({
    url: '/getScoringRules',
    method: 'GET',
    params: { ruleCode }
  })
}

/**
 * @description 查询评分方式
 */
export const getScoringType = () => {
  return request({
    url: '/getScoringType',
    method: 'GET',
    params: {}
  })
}

/**
 * @description 查询专家信息
 * @param batchCode 评价批次编号
 * @param grade 学段
 * @returns Promise
 */
export const getReviewerInfo = (batchCode: string, grade: string) => {
  return request({
    url: '/getReviewerInfo',
    method: 'GET',
    params: { batchCode, grade }
  })
}

/**
 * @description 下载评审专家账号信息表
 * @param batchCode 评价批次编号
 * @returns Promise
 */
export const downloadReviewerInfo = (batchCode: string) => {
  return request({
    url: '/downloadReviewerInfo',
    method: 'POST',
    params: { batchCode },
    responseType: 'blob'
  })
}

/**
 * @description 获取评审资源
 * @param batchCode 评价批次编号
 * @param userCode 评审专家编号
 * @param questionCode 指标编号
 * @returns Promise
 */
export const getResourceInfo = (
  batchCode: string,
  userCode: string,
  questionCode: string
) => {
  return request({
    url: '/getResourceInfo',
    method: 'GET',
    params: {
      batchCode,
      userCode,
      questionCode
    }
  })
}

/**
 * @description 保存评审记录
 * @param batchCode 评价批次编号
 * @param id 资源ID
 * @param json 评审记录
 * @param type 打分方式
 * @param unitCode 学校编号
 * @returns Promise
 */
export const saveScore = (
  batchCode: string,
  type: string,
  json: string,
  id: string | undefined,
  unitCode: string | undefined,
  reviewerCode: string
) => {
  return request({
    url: '/saveScore',
    method: 'POST',
    params: {},
    data: {
      batchCode,
      type,
      json,
      id,
      unitCode,
      reviewerCode
    }
  })
}

/**
 * @description 保存分数审核记录
 * @param batchCode 评价批次编号
 * @param id 资源ID
 * @param json 评审记录
 * @param type 打分方式
 * @param unitCode 学校编号
 * @returns Promise
 */
export const saveScoreReview = (
  batchCode: string,
  type: string,
  json: string,
  id: string,
  unitCode: string | undefined,
  reviewerCode: string
) => {
  return request({
    url: '/saveScoreReview',
    method: 'POST',
    params: {},
    data: {
      batchCode,
      type,
      json,
      id,
      unitCode,
      reviewerCode
    }
  })
}

/**
 * @description 退出系统，注销登录
 * @returns Promise
 */
export const logout = () => {
  return request({
    url: '/logout',
    method: 'GET'
  })
}

/**
 * @description 获取手机短信验证码
 * @param phone 手机号
 * @returns Promise
 */
export const getSmsCode = (phone: string) => {
  return request({
    url: '/getSmsCode',
    method: 'GET',
    params: {
      phone
    },
    headers: {
      isLoading: false
    }
  })
}

/**
 * @description 量表导入模板下载
 * @param batchCode 评价批次编号
 * @returns Promise
 */
export const downloadTemplate = (batchCode: string) => {
  return request({
    url: '/downloadTemplate',
    method: 'GET',
    params: {
      batchCode
    },
    responseType: 'blob'
  })
}

/**
 * @description 量表导入
 * @param formData 文件数据
 * @returns Promise
 */
export const upload = (formData: any) => {
  return request({
    url: '/upload',
    method: 'POST',
    params: {},
    data: formData
  })
}

/**
 * @description 量表导入错误信息下载
 * @param batchCode 评价批次编号
 * @returns Promise
 */
export const downloadErrorExcel = (batchCode: string) => {
  return request({
    url: '/downloadErrorExcel',
    method: 'GET',
    params: {
      batchCode
    },
    responseType: 'blob'
  })
}

/**
 * @description 评价打分结果下载
 * @param batchCode 评价批次编号
 * @param unitCode 组织机构编号
 * @returns Promise
 */
export const downloadReport = (batchCode: string, unitCode?: string) => {
  return request({
    url: '/downloadReport',
    method: 'post',
    params: {
      batchCode,
      unitCode
    },
    responseType: 'blob'
  })
}

/**
 * @description 获取学校上传资源信息
 * @param batchCode 评价批次编号
 * @param unitCode 学校编号
 * @param questionCode 指标编号
 * @returns Promise
 */
export const getSchoolResourceInfo = (
  batchCode: string,
  unitCode: string,
  questionCode: string
) => {
  return request({
    url: '/getSchoolResourceInfo',
    method: 'get',
    params: {
      batchCode,
      unitCode,
      questionCode
    }
  })
}

/**
 * @description 学校上传进度查询
 * @param batchCode 评价批次编号
 * @returns Promise
 */
export const getUploadProgress = (batchCode: string) => {
  return request({
    url: '/getUploadProgress',
    method: 'GET',
    params: {
      batchCode
    }
  })
}

/**
 * @description 未上传学校信息查询
 * @param batchCode 评价批次编号
 * @param questionCode 题目编号
 * @returns Promise
 */
export const getUnaccomplishedUnit = (
  batchCode: string,
  questionCode: string
) => {
  return request({
    url: '/getUnaccomplishedUnit',
    method: 'GET',
    params: {
      batchCode,
      questionCode
    }
  })
}

/**
 * @description 获取评价批次学段信息
 * @param batchCode 评价批次
 * @returns Promise
 */
export const getBatchGradeInfo = (batchCode: string) => {
  return request({
    url: '/getBatchGradeInfo',
    method: 'GET',
    params: {
      batchCode
    }
  })
}

/**
 * @description 查询专家分配信息
 * @param batchCode 评价批次
 * @param grade 学段
 * @returns Promise
 */
export const getDistributeInfo = (batchCode: string, grade: string) => {
  return request({
    url: '/getDistributeInfo',
    method: 'GET',
    params: {
      batchCode,
      grade
    }
  })
}

/**
 * @description 生成专家账号
 * @param batchCode 评价批次
 * @param json 账号设置信息
 * @returns Promise
 */
export const createReviewerInfo = (batchCode: string, json: string) => {
  return request({
    url: '/createReviewerInfo',
    method: 'POST',
    params: {},
    data: {
      batchCode,
      json
    }
  })
}

/**
 * @description 生成评审专家账号和评审资源
 * @param batchCode 评价批次编号
 * @param grade 学段
 * @param questionCode 题目编号
 * @param type 评审方式
 * @param reviewerCodes 专家账号编号
 * @returns Promise
 */
export const createResource = (
  batchCode: string,
  grade: string,
  questionCode: string,
  type: string,
  reviewerCodes: string
) => {
  return request({
    url: '/createResource',
    method: 'POST',
    params: { batchCode, grade, questionCode, type, reviewerCodes },
    data: {}
  })
}

/**
 * @description 获取仲裁资源
 * @param batchCode 评价批次
 * @returns Promise
 */
export const getFinalResource = (batchCode: string) => {
  return request({
    url: '/getFinalResource',
    method: 'GET',
    params: { batchCode }
  })
}

/**
 * @description 保存仲裁打分
 * @param batchCode 评价批次
 * @param unitCode 学校编号
 * @param json 打分json
 * @returns Promise
 */
export const saveFinalScore = (
  batchCode: string,
  unitCode: string,
  json: string
) => {
  return request({
    url: '/saveFinalScore',
    method: 'POST',
    data: {
      batchCode,
      unitCode,
      json
    }
  })
}

/**
 * @description 查询用户列表
 * @param key 查询条件
 * @returns Promise
 */
export const getUserInfo = (key?: string) => {
  return request({
    url: '/getUserInfo',
    method: 'GET',
    params: {
      key
    }
  })
}

/**
 * @description 根据编号获取用户信息
 * @param userCode 用户编号
 * @returns Promise
 */
export const getUserInfoByCode = (userCode: string) => {
  return request({
    url: '/getUserInfoByCode',
    method: 'GET',
    params: {
      userCode
    }
  })
}

interface UserInfoType {
  // 用户编号[修改用户信息时必选]
  userCode?: string
  // 登录账号
  accountName: string
  // 登录密码[新增用户信息是必选]
  accountPass?: string
  // 姓名
  userName: string
  // 所属机构编号[下拉树，从接口/api/getUnitInfo获取]
  unitCode: string
  // 手机号
  userPhone: string
  // 学段编号
  grade: string
  // 角色编号[下拉单选，从接口/api/getRoleInfo获取]
  roleCode: string
  // 启用短信登录[0:不启用;
  smsValidate: number
  // 用户状态[0:禁用;1:启用，默认启用]
  status: number
}

/**
 * @description 新增或者修改用户信息
 * @param params 用户信息
 * @returns Promise
 */
export const saveUserInfo = (params: UserInfoType) => {
  return request({
    url: '/saveUserInfo',
    method: 'GET',
    params
  })
}

/**
 * @description 删除用户信息
 * @param userCode 用户编号
 * @returns Promise
 */
export const deleteUserInfo = (userCode: string) => {
  return request({
    url: '/deleteUserInfo',
    method: 'GET',
    params: {
      userCode
    }
  })
}

/**
 * @description 获取系统角色列表
 * @returns Promise
 */
export const getRoleInfo = () => {
  return request({
    url: '/getRoleInfo',
    method: 'GET',
    params: {}
  })
}

/**
 * @description 根据编号查询角色信息
 * @param roleCode 角色编号
 * @returns Promise
 */
export const getRoleInfoByCode = (roleCode: string) => {
  return request({
    url: '/getRoleInfoByCode',
    method: 'GET',
    params: { roleCode }
  })
}
interface RoleInfoType {
  // 角色编号[修改角色信息时必选]
  code?: string
  // 角色名称
  name: string
  // 备注信息
  desc: string
  // 用户状态[0:禁用;1:启用，默认启用]
  status: string
}

/**
 * @description 新增或修改角色信息
 * @param params 角色参数
 * @returns Promise
 */
export const saveRoleInfo = (params: RoleInfoType) => {
  return request({
    url: '/saveRoleInfo',
    method: 'GET',
    params
  })
}

/**
 * @description 删除角色
 * @param roleCode 角色编号[多个用逗号隔开]
 * @returns Promise
 */
export const deleteRoleInfo = (roleCode: string) => {
  return request({
    url: '/deleteRoleInfo',
    method: 'GET',
    params: { roleCode }
  })
}

/**
 * @description 查询角色关联菜单信息
 * @param roleCode 角色编号
 * @returns Promise
 */
export const getRoleMenuInfo = (roleCode: string) => {
  return request({
    url: '/getRoleMenuInfo',
    method: 'GET',
    params: { roleCode }
  })
}

/**
 * @description 保存角色关联菜单信息
 * @param roleCode 角色编号
 * @param menuCode 菜单编号[多个用逗号隔开]
 * @returns Promise
 */
export const saveRoleMenuInfo = (roleCode: string, menuCode: string) => {
  return request({
    url: '/saveRoleMenuInfo',
    method: 'GET',
    params: { roleCode, menuCode }
  })
}

/**
 * @description 查询指标列表信息
 * @param name 指标名称（模糊查询）
 * @returns Promise
 */
export const getRuleInfo = (name: string) => {
  return request({
    url: '/getRuleInfo',
    method: 'GET',
    params: { name }
  })
}

/**
 * @description 根据编号查询指标信息
 * @param code 指标code
 * @returns Promise
 */
export const getRuleInfoByCode = (code: string) => {
  return request({
    url: '/getRuleInfoByCode',
    method: 'GET',
    params: { code }
  })
}

interface RuleInfoType {
  // 指标编号[修改指标信息时必选]
  code?: string
  // 指标名称
  name: string
  // 备注
  desc: string
}
/**
 * @description 保存指标信息
 * @returns Promise
 */
export const saveRuleInfo = (params: RoleInfoType) => {
  return request({
    url: '/saveRuleInfo',
    method: 'GET',
    params
  })
}

/**
 * @description 删除指标信息
 * @param code 指标code
 * @returns Promise
 */
export const deleteRuleInfo = (code: string) => {
  return request({
    url: '/deleteRuleInfo',
    method: 'GET',
    params: { code }
  })
}

/**
 * @description 查询指标项列表信息
 * @param ruleCode 指标编号
 * @returns Promise
 */
export const getRuleItemInfo = (ruleCode: string) => {
  return request({
    url: '/getRuleItemInfo',
    method: 'GET',
    params: { ruleCode }
  })
}

/**
 * @description 根据编号查询指标项信息
 * @param ruleCode 指标编号
 * @param code 指标项编号
 * @returns Promise
 */
export const getRuleItemInfoByCode = (ruleCode: string, code: string) => {
  return request({
    url: '/getRuleItemInfoByCode',
    method: 'GET',
    params: { ruleCode, code }
  })
}

/**
 * @description 查询学校信息
 * @param grade 学段编号[下拉单选，从接口/api/getGradeInfo获取]
 * @param name 学校名称
 */
export const getInstitutionList = ({
  grade,
  name
}: {
  grade?: string
  name?: string
}) => {
  return request({
    url: '/unit/getSchoolInfo',
    method: 'GET',
    params: { grade, name }
  })
}

/**
 * @description 删除机构信息
 * @param code 机构编号
 * @returns Promise
 */
export const deleteSchoolInfo = (code: string) => {
  return request({
    url: '/deleteSchoolInfo',
    method: 'GET',
    params: { code }
  })
}

/**
 * @description 保存学校信息
 * @param param0 {code: 学校编号[修改学校信息时必选],name: 学校名称,grade: 学段编号[下拉单选，从接口/api/getGradeInfo获取]}
 * @returns Promise
 */
export const saveSchoolInfo = ({
  code,
  name,
  grade,
  areaCode
}: {
  code?: string
  name: string
  grade: string
  areaCode: string
}) => {
  return request({
    url: '/saveSchoolInfo',
    method: 'GET',
    params: { code, name, grade, areaCode }
  })
}

/**
 * @description 查询区县信息
 * @returns Promise
 */
export const getAreaInfo = () => {
  return request({
    url: '/getAreaInfo',
    method: 'GET',
    params: {}
  })
}

/**
 * @description 根据编号查询学校信息
 * @param code 学校编号
 * @returns Promise
 */
export const getSchoolInfoByCode = (code: string) => {
  return request({
    url: '/getSchoolInfoByCode',
    method: 'GET',
    params: { code }
  })
}

/**
 * @description 删除指标项信息
 * @param criteriaCode 指标编号
 * @param code 指标项编号[多个用逗号隔开]
 * @returns
 */
export const deleteRuleItemInfo = (criteriaCode: string, code: string) => {
  return request({
    url: '/deleteRuleItemInfo',
    method: 'GET',
    params: { criteriaCode, code }
  })
}

interface RuleItemInfoType {
  // 指标编号
  criteriaCode: string
  // 指标项编号[修改指标项信息时必选]
  code?: string
  // 指标号
  title: string
  // 名称
  name: string
  // 分值
  point: string
}
export const saveRuleItemInfo = (params: RuleItemInfoType) => {
  return request({
    url: '/saveRuleItemInfo',
    method: 'GET',
    params
  })
}

/**
 * @description 查询评价方式
 * @returns Promise
 */
export const getEvaluateType = () => {
  return request({
    url: '/getEvaluateType',
    method: 'GET',
    params: {}
  })
}

/**
 * @description 查询评价主题
 * @returns Promise
 */
export const getEvaluateBody = () => {
  return request({
    url: '/getEvaluateBody',
    method: 'GET',
    params: {}
  })
}

/**
 * @description 获取评分规则
 * @returns Promise
 */
export const getScoreRuleCode = () => {
  return request({
    url: '/getScoreRuleCode',
    method: 'GET',
    params: {}
  })
}

/**
 * @description 发送及时预警
 * param unitName 学校名称
 * param unitCode 机构编号
 * param date 截止日期
 * @returns
 */
export const sendWarningSMS = (
  unitName: string,
  unitCode: string,
  date: string
) => {
  return request({
    url: '/sendWarningSMS',
    method: 'GET',
    params: { unitName, unitCode, date }
  })
}
